
/* ====================================================================
   Copyright 2002-2004   Apache Software Foundation

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
        


package org.vectrics.recruiter.user.resume;


import java.io.ByteArrayOutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;

import org.apache.log4j.Logger;
import org.apache.poi.hdf.extractor.WordDocument;
import org.vectrics.SystemException;


public class WordDocumentReader {
	private transient final Logger log = Logger.getLogger(this.getClass()); 
	
	public String wordDocToString(String fileName) {
        StringBuffer newString = new StringBuffer();
	    try
	    {
	        WordDocument file = new WordDocument(fileName);
	        Date now = new Date();
	        //FileOutputStream outStream = new FileOutputStream("poi-text-" + now.getTime() +  ".txt", true);
	        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
	        OutputStreamWriter writer = new OutputStreamWriter(outStream);
	        file.writeAllText(writer);
	        writer.close();
	        outStream.close();
	        String docString = outStream.toString();
	        if (log.isDebugEnabled())
	        	log.debug("Before removing extra characters: " + docString);
	        // For some reason, the POI Word Document reader places a bunch of 
	        // #7's together where other formatting use to be.  Remove these #7's
	        // and replace a series of #7's with a single space.
	        for (int i = 0; i < docString.length(); i++) {
	        	char c = docString.charAt(i);
	        	if ((int)c != 7) {
	        		newString.append(c);
	        	} else {
		          	// If next character is not #7, then make a space to take a place of all the #7's in a row.
		          	if (i < docString.length() - 1) {
		          		char nextChar = docString.charAt(i + 1);
		          		if ((int)nextChar != 7)
		          			newString.append("\r\n");
		          	}	
	        	}
	        	
	        }
	        
	        //System.out.println(newString.toString());
	        //file.closeDoc();
	    }
	    catch(Exception e)  {
	        throw new SystemException("Exception reading document: " + fileName, e);
	    }
	    if (log.isDebugEnabled()) {
	    	String str = newString.toString();
	    	log.debug("Word document content (after removing #7's): " + newString.toString());
	    	
	    	log.debug("Asci output: ");
	    	for (int i = 0; i < str.length(); i++) {
	    		char c = str.charAt(i);
	    		log.debug("char " + i + ": " + (int)c + "   -> '" + c + "'");
	    	}
	    }
	    
	    return(newString.toString());
	}
}
